<template>
    <div>
        <div class="goods">
            <div class="menu-wrapper" ref="left">
                <ul class="content" ref="leftUl">
                    <li class="menu-item" v-for="(item,index) in goods" :key="index"
                        :class="{current:index===Index}" v-on:click="toTop(index)">
                        <span class="text bottom-border-1px">
                            <img class="icon"
                                 :src="item.icon" v-if="item.icon">
                            {{item.name}}
                        </span>

                    </li>

                </ul>
            </div>
            <div class="foods-wrapper" ref="right">
                <ul class="content" ref="rightUl">
<!--                    goods为分类列表，item为每个分类-->
                    <li class="food-list-hook" v-for="(item,index) in goods" :key="index">
                        <h1 class="title">{{item.name}}</h1>
                        <ul>
<!--                            item1为当前分类下的每种食物商品-->
                            <li class="food-item bottom-border-1px" v-for="(item1,index1) in item.foods" @click="saveFood(item1)" :key="index1">
                                <div class="icon">
                                    <img width="57" height="57"
                                         :src="item1.icon">
                                </div>
                                <div class="content">
                                    <h2 class="name">{{item1.name}}</h2>
                                    <p class="desc">{{item1.description}}</p>
                                    <div class="extra">
                                        <span class="count">月售{{item1.sellCount}}份</span>
                                        <span>好评率{{item1.rating}}%</span>
                                    </div>
                                    <div class="price">
                                        <span class="now">￥{{item1.price}}</span>
                                        <span class="old" v-if="item1.oldPrice">￥{{item1.oldPrice}}</span>
                                    </div>
                                    <div class="cartcontrol-wrapper">
                                        <CartControl :food="item1"></CartControl>
                                    </div>
                                </div>
                            </li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
        <Food :food="food" ref="food"></Food>
    </div>
</template>

<script>
    import {mapState} from 'vuex'
    import BScroll from 'better-scroll'
    import CartControl from "../../../components/CartControl/CartControl";
    import Food from "../../../components/Food/Food";
    export default {
        name: "ShopGoods",
        components:{
            CartControl,
            Food
        },
        data() {
            return {
                Index: 0,
                distance: [],
                itemHeight: 0,
                food:{},
            }
        },
        computed: {
            ...mapState({
                goods:state=>state.shop.shop.goods//拿到每一种分类（左侧导航用）
            }),
        },
        mounted() {
            this.initScroll()
            this.initIndex()
        },
        methods: {
            initScroll() {

                //初始化左侧导航滑动
                this.Bs = new BScroll(this.$refs.left, {
                    pullUpLoad: true,
                    click: true
                })
                //初始化右侧导航滑动
                this.Bs1 = new BScroll(this.$refs.right, {
                    pullUpLoad: true,
                    click: true
                })
                //为右导航添加滑动事件
                this.Bs1.on('scrollEnd',(pos)=>{
                    this.distance.forEach((item,index)=>{
                        if(Math.abs(pos.y)>=item-(1/3)*this.$refs.right.clientHeight){
                            //通过this.Index选中左侧列表
                            this.Index=index
                            //得到右侧列表超出容器部分占容器的比例
                            const Mag=-pos.y/this.$refs.rightUl.clientHeight
                            //计算得出左侧列表上滑超出容器部分需要移动的距离
                            const magLeft=(this.$refs.left.clientHeight-this.$refs.leftUl.clientHeight)*Mag
                            this.Bs.scrollTo(0,magLeft,500)
                        }
                    })

                })
            },
            initIndex() {
                //初始化每个分类的滑动距离编号，如果滑动大距离于当前编号距离，这赋值为当前编号
                this.$refs.rightUl.children.forEach((item) => {
                    this.distance.push(this.itemHeight)
                    this.itemHeight += item.clientHeight
                })
            },
            toTop(index){
                //左侧导航点击滑动事件
                //得到序号对应的右侧ul偏移量
                const y=-this.distance[index]+(1/3)*this.$refs.right.clientHeight
               //点击到左侧列表指定位置
                this.Index=index
                this.Bs1.scrollTo(0,y,500)
            },
            saveFood(food){
                //food为当前页面的每一事物，用于点击图片放大事件
                this.food=food
                this.$refs.food.toggleShow()
            },

        },
        updated() {
            //试图层数据发生改变时触发，也就是刷新浏览器会触发，重新为每种分类编号
            this.initIndex()
        },
        watch: {
            goods() {
                //当分类列表数据发生变化时，重新计算滚动条高度
                this.$nextTick(() => {
                    this.Bs.refresh()
                    this.Bs1.refresh()
                })
            }
        }

    }
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
    @import "../../../common/stylus/minix.styl"
    .goods
        display: flex
        position: absolute
        top: 225px
        bottom: 46px
        width: 100%
        background: #fff;
        overflow: hidden

        h1
            margin-top 0

        .menu-wrapper
            flex: 0 0 80px
            width: 80px
            background: #f3f5f7

            .menu-item
                display: table
                height: 54px
                padding: 0 12px
                line-height: 14px
                transition none
                &.current
                    position: relative
                    z-index: 10
                    margin-top: -1px
                    background: #fff
                    color: $green
                    font-weight: 700

                    .text
                        border-none()

                .icon
                    display: inline-block
                    vertical-align: top
                    width: 12px
                    height: 12px
                    margin-right: 2px
                    background-size: 12px 12px
                    background-repeat: no-repeat

                .text
                    display: table-cell
                    width: 56px
                    vertical-align: middle
                    bottom-border-1px(rgba(7, 17, 27, 0.1))
                    font-size: 12px

        .foods-wrapper
            flex: 1

            .title
                padding-left: 14px
                height: 26px
                line-height: 26px
                border-left: 2px solid #d9dde1
                font-size: 12px
                color: rgb(147, 153, 159)
                background: #f3f5f7

            .food-item
                display: flex
                margin: 18px
                padding-bottom: 18px
                bottom-border-1px(rgba(7, 17, 27, 0.1))

                &:last-child
                    border-none()
                    margin-bottom: 0

                .icon
                    flex: 0 0 57px
                    margin-right: 10px

                .content
                    flex: 1

                    .name
                        margin: 2px 0 8px 0
                        height: 14px
                        line-height: 14px
                        font-size: 14px
                        color: rgb(7, 17, 27)

                    .desc, .extra
                        line-height: 10px
                        font-size: 10px
                        color: rgb(147, 153, 159)

                    .desc
                        line-height: 12px
                        margin-bottom: 8px

                    .extra
                        .count
                            margin-right: 12px

                    .price
                        font-weight: 700
                        line-height: 24px

                        .now
                            margin-right: 8px
                            font-size: 14px
                            color: rgb(240, 20, 20)

                        .old
                            text-decoration: line-through
                            font-size: 10px
                            color: rgb(147, 153, 159)

                    .cartcontrol-wrapper
                        position: absolute
                        right: 0
                        bottom: 12px
</style>